from typing import List
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_assessment import RiskAssessment
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_stock_day import StrategyStockDay
from service.strategy.base_strategy import BaseStrategy


# 实现策略类：判断当前股票是否在过去3天内多次涨停，且前3-5天没有涨停
class MultipleLimitUpStrategy(BaseStrategy):
    """
    判断股票在过去3天内是否多次涨停，且前3-5天没有涨停。
    每涨停一天，得分 +1。
    """

    def analyze(self, trade_info_list: List[StrategyStockDay]) -> RiskAssessment:
        """
        分析过去3天内是否多次涨停，且前3-5天没有涨停
        """
        node_point = 0
        description = ""

        # 确保至少有5天的交易数据
        if len(trade_info_list) >= 5:
            # 获取过去5天的股票交易数据
            recent_trades = trade_info_list[-5:]

            # 判断过去3天是否连续涨停，并且前3-5天没有涨停
            limit_up_count = 0  # 统计涨停的天数
            no_limit_up_in_past = True  # 标记前3-5天没有涨停

            # 过去3天内的交易数据
            for i in range(3):
                trade = recent_trades[-(i + 1)]
                if (
                    trade.close == trade.high
                ):  # 判断是否涨停，假设涨停的条件是收盘价等于最高价
                    limit_up_count += 1

            # 检查前3-5天是否没有涨停
            for i in range(3, 5):
                trade = recent_trades[-(i + 1)]
                if trade.close == trade.high:
                    no_limit_up_in_past = False
                    break

            # 判断是否符合条件：过去3天涨停且前3-5天没有涨停
            if limit_up_count >= 2 and no_limit_up_in_past:
                node_point = limit_up_count  # 每涨停一天得分 +1
                description = f"Stock had {limit_up_count} limit ups in the last 3 days, and no limit up in the past 3-5 days."

        # 创建策略信息对象
        return RiskAssessment(
            stock_code=trade_info_list[0].stock_code,
            description=description,
            confif=self.strategyConfig,
            node_point=node_point,
        )

    def strategyConfig(self) -> dict:
        """
        返回策略的配置
        """
        return StrategyInfoMetadata(
            strategy_code="MLUS",
            strategy_name="Multiple Limit Up Strategy",
            strategy_group=1,  # 假设是关注型策略
            strategy_type="Limit Up Trend",
            analysis_day=5,
            strategy_level=2,
        )
